package com.uniqueyi.baselib.utilsbase.appmanager;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/**
 * Created by 玖 on 2016/9/2.
 */
public class AppManager {

    private static Stack<Activity> activitytStack;
    private static AppManager appManager;

    private AppManager() {

    }

    /**
     * 单例模式
     *
     * @return
     */
    public static AppManager getInstance() {
        if (appManager == null) {
            synchronized (AppManager.class) {
                if (appManager == null)
                    appManager = new AppManager();
            }
        }
        return appManager;
    }

    /**
     * 添加activyt入栈
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (activitytStack == null) {
            activitytStack = new Stack<Activity>();
        }
        activitytStack.add(activity);
    }

    /**
     * 获取栈顶activyt
     *
     * @return
     */
    public Activity currentActivity() {
        return activitytStack.lastElement();
    }

    /**
     * 结束activity
     */
    public void finishActivity() {
        Activity activity = activitytStack.lastElement();
        if (activity != null) {
            finishActivity(activity);
        }
    }

    /**
     * 获取指定的activity
     *
     * @param cls
     */
    public Activity getActivity(Class<?> cls) {
        for (Activity activity : activitytStack) {
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 结束指定的activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activitytStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     *
     * @param cls
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activitytStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 获取栈
     *
     * @return
     */
    public static Stack<Activity> getActivityStack() {
        return activitytStack;
    }

    /**
     * 结束所有activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activitytStack.size(); i < size; i++) {
            if (null != activitytStack.get(i)) {
                activitytStack.get(i).finish();
            }
        }
        activitytStack.clear();
    }

    /**
     * 退出程序
     *
     * @param context
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context
                    .ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
        }
    }

}
